package com.etb.wisdom.eframe.auth.action;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.etb.core.utils.EncryptionUtil;
import com.etb.web.action.ETBAction;
import com.etb.web.utils.SessionUtil;
import com.etb.web.utils.WebUtil;
import com.etb.wisdom.eframe.auth.service.AuthService;
import com.etb.wisdom.eframe.model.EtbUser;

@Action("login")
public class LoginAction extends ETBAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -826266687499174050L;
	
	@Autowired
	private AuthService authService;

	public void user() {
		Integer result = 0;
		String loginname = request.getParameter("loginname");
		String password = request.getParameter("password");
		EtbUser user = new EtbUser();
		user.setLoginname(loginname);
		service.extend(user);
		if(!user.exist()) {
			result = 1;//登录名不存在
		} else if(!user.getPassword().equals(EncryptionUtil.encrypt(password))) {
			result = 2;//密码不正确
		} else {
			SessionUtil.setParty(authService.initParty(user));
		}
		WebUtil.render(result.toString());
	}
	
	public void userLogout() {
		session.invalidate();
	}
}
